package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.product.converter.dto.TrademarkConverter;
import com.cskaoyan.mall.product.dto.TrademarkDTO;
import com.cskaoyan.mall.product.dto.TrademarkPageDTO;
import com.cskaoyan.mall.product.mapper.TrademarkMapper;
import com.cskaoyan.mall.product.model.Trademark;
import com.cskaoyan.mall.product.query.TrademarkParam;
import com.cskaoyan.mall.product.service.TrademarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TrademarkServiceImpl implements TrademarkService {
    @Autowired
    TrademarkMapper trademarkMapper;
    @Autowired
    TrademarkConverter trademarkConverter;

    @Override
    public TrademarkDTO getTrademarkByTmId(Long tmId) {
        Trademark trademark = trademarkMapper.selectById(tmId);
        TrademarkDTO trademarkDTO = trademarkConverter.trademarkPO2DTO(trademark);
        return trademarkDTO;
    }

    @Override
    public TrademarkPageDTO getPage(Page<Trademark> pageParam) {
        //根据分页获取对应的内容
        trademarkMapper.selectPage(pageParam, null);

        List<TrademarkDTO> trademarkDTOList=trademarkConverter.trademarkPOs2DTOs(pageParam.getRecords());


        TrademarkPageDTO trademarkPageDTO=TrademarkPageDTO
                .builder()
                .records(trademarkDTOList)
                .total((int) pageParam.getTotal())
                .build();

        return trademarkPageDTO;
    }

    @Override
    public void save(TrademarkParam trademarkParam) {
        Trademark trademark = trademarkConverter.trademarkParam2Trademark(trademarkParam);
        //数据保存
        if (trademarkParam.getId()!=null){
            trademarkMapper.updateById(trademark);
        }else {
            trademarkMapper.insert(trademark);
        }
    }

    @Override
    public void updateById(TrademarkParam trademarkParam) {
        Trademark trademark = trademarkConverter.trademarkParam2Trademark(trademarkParam);
        trademarkMapper.updateById(trademark);
    }

    @Override
    public void removeById(Long id) {
        trademarkMapper.deleteById(id);
    }
}
